﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

namespace Z16.BLI
{
    public class Cliente
    {
        #region "Variaveis"

        private string strNomeCliente;
        public string StrNomeCliente
        {
            get { return strNomeCliente; }
            set { strNomeCliente = value; }
        }

        #endregion


        #region "Metodos"

        public string incCliente(string strNomeCliente, int? codCli)
        {
            try
            {
                SqlParameter[] pms =
                    {
                        new SqlParameter("@NOMECLIENTE", strNomeCliente),
                        new SqlParameter("@CODCLI", codCli)
                    };

                if (codCli == null)
                {
                    SqlHelper.ExecuteNonQuery(SqlHelper.GetSQLConnection(), CommandType.StoredProcedure, "procIncCliente", pms);

                    return "0;Cliente adicionado com sucesso!";
                }
                else
                {
                    SqlHelper.ExecuteNonQuery(SqlHelper.GetSQLConnection(), CommandType.StoredProcedure, "procEditCliente", pms);

                    return "0;Cliente editado com sucesso!";
                }              
            }
            catch (Exception ex)
            {
                return "1;Erro ao inserir Cliente: " + ex.Message;
            }
        }

        public DataTable ListarClientes(int? codCli,string cliente)
        {
            DataTable cc = new DataTable();

            SqlParameter[] pms =
                    {
                        new SqlParameter("@codCli",codCli),
                        new SqlParameter("@cliente",cliente)
                    };

            try
            {
                SqlDataReader retorno = SqlHelper.ExecuteReader(SqlHelper.GetSQLConnection(), CommandType.StoredProcedure, "procSelCliente", pms);

                cc.Load(retorno);
            }
            catch (Exception ex)
            {
                throw;
            }
            return cc;
        }

        public int ExcluirCliente(int codigo)
        {
            SqlConnection conn = new SqlConnection(SqlHelper.GetSQLConnection());
            conn.Open();
            SqlCommand dCmd = new SqlCommand("DeletarCliente", conn);
            dCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dCmd.Parameters.AddWithValue("@codigo", codigo);
                return dCmd.ExecuteNonQuery();
            }
            catch
            {
                throw;
            }
            finally
            {
                dCmd.Dispose();
                conn.Close();
                conn.Dispose();
            }
        }

        #endregion
    }
}